<HTML>
<HEAD>
<link rel=stylesheet type="text/css" href="styles.css">
<TITLE>Using Arrays in Simkin</TITLE>
</HEAD>
<BODY>

<H1>
Using Arrays in Simkin</H1>
<hr>
Simkin supports a special syntax to make accessing collections more convenient. For example:
<pre>
var=array[1];
</pre>
Simkin for Java supports the array index feature in one of two ways:
<ol>
<li>If the underlying object is a native Java array, the interpreter will automatically retrieve or set the value in the array. The Interpreter also supports the <b>length</b> or <b>numChildren</b> attribute of a native array, which will return the size of the array</li>
<li>If the underlying object implements the <a href="api/simkin/Executable.html">Executable</a> interface, the Interpreter will call the <b>setValueAt</b> or <b>getValueAt</b> methods</li>
</ol>
<p>For example, if <b>array</b> implements the <a href="api/simkin/Executable.html">Executable</a> interface, the the following results in a call to <b>setValueAt</b>:
<pre>
array[1]="foo";
</pre>
and this example results in a call to <b>getValueAt</b>:
<pre>
var=array[1];
</pre>
<p>The script syntax allows for arbitrary expressions within the <b>[ ]</b>, so for example the following are legal syntax:
<pre>
var=array[1];
var=array["key"];
</pre>
If the underlying array is a native Java array, the interpreter will always convert the expression to a number, which is zero-based. 
<p>
If the underlying object implements the <a href="api/simkin/Executable.html">Executable</a> interface, the value is passed in "as-is" to <b>setValueAt</b> or <b>getValueAt</b>. This means that the underlying Java classes can expose collections which are keyed on any kind of value - so that ordinary Vectors, and Hashtables or Associative Arrays can all be supported.
<p>
Simkin is supplied with two classes which have array index support, <a href="api/simkin/TreeNodeObject.html">TreeNodeObject</a> and <a href="api/simkin/XMLElementObject.html">XMLElementObject</a>. These both convert the array index to a numeric value before using it as an index into their collection. They also both assume the index is <b>zero based</b>. Both classes also support a field called <b>numChildren</b> which returns the number of elements in each collection.
</body>
</html>